package com.ruoyi.lxy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.lxy.domain.*;
import com.ruoyi.lxy.domain.vo.TDependantVo;
import com.ruoyi.lxy.domain.vo.TRosterVo;
import com.ruoyi.lxy.mapper.TDependantMapper;
import com.ruoyi.lxy.zust.domain.TStaff;
import com.ruoyi.lxy.zust.service.impl.TDepartmentServiceImpl;
import com.ruoyi.lxy.zust.service.impl.TStaffServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.lxy.domain.bo.TUserBo;
import com.ruoyi.lxy.domain.vo.TUserVo;
import com.ruoyi.lxy.mapper.TUserMapper;
import com.ruoyi.lxy.service.ITUserService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-24
 */
@RequiredArgsConstructor
@Service
public class TUserServiceImpl implements ITUserService {

    private final TUserMapper baseMapper;
    final TStaffServiceImpl  tStaffService;
    final TDepartmentServiceImpl tdepartmentService;//zust中的接口
    final TDependantMapper tDependantMapper;

    /**
     * 查询用户
     */
    @Override
    public TUserVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户列表
     */
    @Override
    public TableDataInfo<TUserVo> queryPageList(TUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TUser> lqw = buildQueryWrapper(bo);
        Page<TUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户列表
     */
    @Override
    public List<TRosterVo> queryList(Long timeId) {
//        LambdaQueryWrapper<TUser> lqw = buildQueryWrapper(bo);
//        LambdaQueryWrapper<TUser> lqw = Wrappers.lambdaQuery();
//        lqw.inSql(TUser::getId,"select user_id from t_select_user where time_id= '"+timeId+"' and  del_flag=0 ");
        List<TUser> tUser=baseMapper.selectUserListByTimeId(timeId);
        List<TRosterVo> tRosterVos = new ArrayList<>();
        for (int i=0;i<tUser.size();i++){
            TStaff tStaff=tStaffService.selectByZgh(tUser.get(i).getZgh());
            TRosterVo userVo=new TRosterVo();
            BeanUtils.copyProperties(tUser.get(i),userVo);

            userVo.setName(tStaff.getXM());
            if(tStaff.getXBDM().equals("1"))
                userVo.setGender("男");
            else
                userVo.setGender("女");
            userVo.setDepartment(tdepartmentService.getDepartment(tUser.get(i).getZgh()));
            userVo.setFlag(0);
            tRosterVos.add(userVo);

            //根据userId和timeId选择出department
            List<TDependant> tDependants = tDependantMapper.selectDependantListByTimeIdAndUserId(timeId,tUser.get(i).getId());
            for(int j=0;j<tDependants.size();j++){
                TRosterVo dependantVo=new TRosterVo();
                BeanUtils.copyProperties(tDependants.get(j),dependantVo);
                dependantVo.setZgh(tStaff.getXM());
                dependantVo.setFlag(1);
                tRosterVos.add(StateTransitionVo(dependantVo));
            }

        }

        return tRosterVos;
    }

    private TRosterVo StateTransitionVo(TRosterVo tRosterVo) {
        if(tRosterVo.getType()!=null){
            switch (tRosterVo.getType()){
                case 0:
                    tRosterVo.setTypeCode("成人");
                    break;
                case 1:
                    tRosterVo.setTypeCode("儿童");
                    break;
                case 2:
                    tRosterVo.setTypeCode("老人");
                    break;
            }
        }

        if(tRosterVo.getNeedBed()!=null){
            switch (tRosterVo.getNeedBed()){
                case 0:
                    tRosterVo.setNeedBedCode("不需要");
                    break;
                case 1:
                    tRosterVo.setNeedBedCode("需要");
                    break;
            }
        }

        return tRosterVo;
    }

    private LambdaQueryWrapper<TUser> buildQueryWrapper(TUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getZgh()), TUser::getZgh, bo.getZgh());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), TUser::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getIdcard()), TUser::getIdcard, bo.getIdcard());
        return lqw;
    }

    /**
     * 新增用户
     */
    @Override
    public Boolean insertByBo(TUserBo bo) {
        TUser add = BeanUtil.toBean(bo, TUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户
     */
    @Override
    public Boolean updateByBo(TUserBo bo) {
        TUser update = BeanUtil.toBean(bo, TUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<TRosterVo> selectUserListByTimeId(Long timeId, PageQuery pageQuery) {
        List<TUser> tUser=baseMapper.selectUserListByTimeId(timeId);
        List<TRosterVo> tRosterVos = new ArrayList<>();
        for (int i=0;i<tUser.size();i++){
            TStaff tStaff=tStaffService.selectByZgh(tUser.get(i).getZgh());
            TRosterVo userVo=new TRosterVo();
            BeanUtils.copyProperties(tUser.get(i),userVo);

            userVo.setName(tStaff.getXM());
            if(tStaff.getXBDM().equals("1"))
                userVo.setGender("男");
            else
                userVo.setGender("女");
            userVo.setDepartment(tdepartmentService.getDepartment(tUser.get(i).getZgh()));
            userVo.setFlag(0);
            tRosterVos.add(userVo);

            //根据userId和timeId选择出department
            List<TDependant> tDependants = tDependantMapper.selectDependantListByTimeIdAndUserId(timeId,tUser.get(i).getId());
            for(int j=0;j<tDependants.size();j++){
                TRosterVo dependantVo=new TRosterVo();
                BeanUtils.copyProperties(tDependants.get(j),dependantVo);
                dependantVo.setZgh(tStaff.getXM());
                dependantVo.setFlag(1);
                tRosterVos.add(dependantVo);
            }


        }
        return TableDataInfo.build(tRosterVos);
    }
}
